package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;

import com.stark.calculator.mortgage.model.LoanModel;

import java.math.BigDecimal;
import java.math.RoundingMode;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityHouseResultBinding;
import stark.common.basic.event.EventStatProxy;

public class HouseResultActivity extends BaseAc<ActivityHouseResultBinding> {

    public static LoanModel loanModel = null;
    private double capResultMoney = 0;
    private double capTotalInterestMoney = 0;
    private double capTotalPayMoney = 0;


    @Override
    protected int onCreate() {
        return R.layout.activity_house_result;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(this, mDataBinding.event5Container);
        mDataBinding.ivHouseResultBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.rlHouseResultGjjView.setVisibility(View.GONE);
        mDataBinding.rlHouseResultSydView.setVisibility(View.GONE);
        if (loanModel.getFundAmount() == 0) {
            mDataBinding.rlHouseResultSydView.setVisibility(View.VISIBLE);
            calculateResult1(loanModel.getAmount(), loanModel.getRate(), loanModel.getYearCount());
        } else if (loanModel.getAmount() == 0) {
            mDataBinding.rlHouseResultGjjView.setVisibility(View.VISIBLE);
            calculateResult2(loanModel.getFundAmount(), loanModel.getFundRate(), loanModel.getYearCount());
        }else {
            mDataBinding.rlHouseResultSydView.setVisibility(View.VISIBLE);
            calculateResult1(loanModel.getAmount(), loanModel.getRate(), loanModel.getYearCount());
            mDataBinding.rlHouseResultGjjView.setVisibility(View.VISIBLE);
            calculateResult2(loanModel.getFundAmount(), loanModel.getFundRate(), loanModel.getYearCount());
        }
        //首月还款
        mDataBinding.tvHouseResul1.setText(String.format("%.2f", capResultMoney));
        //总还款
        mDataBinding.tvHouseResult3.setText(getString(R.string.house_result_text8_def)
                + " " + String.format("%.2f", capTotalPayMoney));
        //利息总额
        mDataBinding.tvHouseResult2.setText(getString(R.string.house_result_text7_def)
                + " " + String.format("%.2f", capTotalInterestMoney));
    }

    @Override
    protected void initData() {

    }

    @SuppressLint("DefaultLocale")
    protected void calculateResult1(float amount, float rate, int year) {
        // 基础数据
        BigDecimal rateRatio = new BigDecimal(100);
        BigDecimal moneyRatio = new BigDecimal(10000);
        BigDecimal monthRatio = new BigDecimal(12);
        BigDecimal foundRatio = new BigDecimal(1);
        BigDecimal towNumber = new BigDecimal(2);

        // 获取月利率
        BigDecimal yearRateNumber = new BigDecimal(rate);
        yearRateNumber = yearRateNumber.divide(rateRatio);
        BigDecimal monthRateNumber = yearRateNumber.divide(monthRatio, 18, RoundingMode.CEILING);

        // 获取还款月数
        BigDecimal yearCountNumber = new BigDecimal(year);
        BigDecimal monthCountNumber = yearCountNumber.multiply(monthRatio);

        // 贷款总金额
        BigDecimal loanMoneyNumber = new BigDecimal(amount);
        loanMoneyNumber = loanMoneyNumber.multiply(moneyRatio);

        // 等额本息计算公式：〔贷款本金×月利率×（1＋月利率）＾还款月数〕÷〔（1＋月利率）＾还款月数－1〕


        //贷款总额
        mDataBinding.tvHouseResultSyd1.setText(String.format("%.0f", amount) + getString(R.string.unit_wanyuan));
        //贷款年限
        mDataBinding.tvHouseResultSyd3.setText(year + getString(R.string.unit_year));
        //利率
        mDataBinding.tvHouseResultSyd2.setText(String.format("%.1f", rate) + getString(R.string.unit_percent));

        //总还款 String.format("%.2f", totalPayMoney)
        //利息总额 String.format("%.2f", totalInterestMoney)

        // 等额本金计算公式：每月还款金额 = （贷款本金 / 还款月数）+（本金 — （（贷款本金 / 还款月数 ）* 第几期）×每月利率 。

        // 1.（贷款本金 / 还款月数）
        BigDecimal cap1Number = loanMoneyNumber.divide(monthCountNumber, 18, RoundingMode.CEILING);
        // 3.本金 × 每月利率
        BigDecimal cap3Number = loanMoneyNumber.multiply(monthRateNumber);
        // 首月还款
        BigDecimal capFirstMonthPayMoneyNumber = cap1Number.add(cap3Number);
        // 还款总利息
        BigDecimal totalInterestNumber = loanMoneyNumber.multiply(monthRateNumber);
        totalInterestNumber = totalInterestNumber.multiply(monthCountNumber.add(foundRatio));
        totalInterestNumber = totalInterestNumber.divide(towNumber, 18, RoundingMode.CEILING);

        capResultMoney += capFirstMonthPayMoneyNumber.doubleValue();
        capTotalInterestMoney += totalInterestNumber.doubleValue();
        capTotalPayMoney += capTotalInterestMoney + amount*10000;
    }

    @SuppressLint("DefaultLocale")
    protected void calculateResult2(float amount, float rate, int year) {
        // 基础数据
        BigDecimal rateRatio = new BigDecimal(100);
        BigDecimal moneyRatio = new BigDecimal(10000);
        BigDecimal monthRatio = new BigDecimal(12);
        BigDecimal foundRatio = new BigDecimal(1);
        BigDecimal towNumber = new BigDecimal(2);

        // 获取月利率
        BigDecimal yearRateNumber = new BigDecimal(rate);
        yearRateNumber = yearRateNumber.divide(rateRatio);
        BigDecimal monthRateNumber = yearRateNumber.divide(monthRatio, 18, RoundingMode.CEILING);

        // 获取还款月数
        BigDecimal yearCountNumber = new BigDecimal(year);
        BigDecimal monthCountNumber = yearCountNumber.multiply(monthRatio);

        // 贷款总金额
        BigDecimal loanMoneyNumber = new BigDecimal(amount);
        loanMoneyNumber = loanMoneyNumber.multiply(moneyRatio);

        //贷款总额
        mDataBinding.tvHouseResultGjj1.setText(String.format("%.0f", amount) + getString(R.string.unit_wanyuan));
        //贷款年限
        mDataBinding.tvHouseResultGjj3.setText(year + getString(R.string.unit_year));
        //利率
        mDataBinding.tvHouseResultGjj2.setText(String.format("%.1f", rate) + getString(R.string.unit_percent));


        //每月还款 String.format("%.2f", bxMPayMoney)
        //总还款 String.format("%.2f", totalPayMoney)
        //利息总额 String.format("%.2f", totalInterestMoney)


        // 等额本金计算公式：每月还款金额 = （贷款本金 / 还款月数）+（本金 — （（贷款本金 / 还款月数 ）* 第几期）×每月利率 。

        // 1.（贷款本金 / 还款月数）
        BigDecimal cap1Number = loanMoneyNumber.divide(monthCountNumber, 18, RoundingMode.CEILING);
        // 3.本金 × 每月利率
        BigDecimal cap3Number = loanMoneyNumber.multiply(monthRateNumber);
        // 首月还款
        BigDecimal capFirstMonthPayMoneyNumber = cap1Number.add(cap3Number);
        // 还款总利息
        BigDecimal totalInterestNumber = loanMoneyNumber.multiply(monthRateNumber);
        totalInterestNumber = totalInterestNumber.multiply(monthCountNumber.add(foundRatio));
        totalInterestNumber = totalInterestNumber.divide(towNumber, 18, RoundingMode.CEILING);

         capResultMoney += capFirstMonthPayMoneyNumber.doubleValue();
         capTotalInterestMoney += totalInterestNumber.doubleValue();
         capTotalPayMoney += capTotalInterestMoney + amount*10000;

    }

}